package com.wing.device.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.device.dao.DeviceWorkmanshipDao;
import com.wing.device.model.entity.DeviceCategory;
import com.wing.device.model.entity.DeviceWorkmanship;
import com.wing.device.model.request.DeviceCategoryForm;
import com.wing.device.model.request.DeviceWorkmanshipForm;
import com.wing.device.model.response.DeviceCategoryVO;
import com.wing.device.model.response.DeviceWorkmanshipVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 设备加工工艺-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeviceWorkmanshipServiceImpl extends ServiceImpl<DeviceWorkmanshipDao, DeviceWorkmanship> implements DeviceWorkmanshipService {

    private static long ROOT_ID=-1l;

    @Override
    public int add(DeviceWorkmanshipForm deviceWorkmanshipForm) {

        //校验

        DeviceWorkmanship deviceWorkmanship =new DeviceWorkmanship();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DeviceWorkmanship.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceWorkmanshipForm, deviceWorkmanship, copyOptions);
        if (deviceWorkmanshipForm.getParentId()!=ROOT_ID){
            DeviceWorkmanship deviceWorkmanshipParent=new DeviceWorkmanship();
            deviceWorkmanshipParent.setId(deviceWorkmanshipForm.getParentId());
            deviceWorkmanshipParent.setHasChild(true);
            if (deviceWorkmanshipForm.getIsDeleted()){
                deviceWorkmanshipParent.setHasEnableChild(true);
            }
            baseMapper.updateById(deviceWorkmanshipParent);
        }
        return baseMapper.insert(deviceWorkmanship);
    }

    @Override
    public List<DeviceWorkmanshipVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeviceWorkmanshipVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeviceWorkmanshipVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeviceWorkmanshipVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int del(Long id) {
        DeviceWorkmanshipVO deviceWorkmanshipVO=baseMapper.get4appById(id);
        if (deviceWorkmanshipVO!=null){
            int delCount=baseMapper.deleteById(id);
            if (deviceWorkmanshipVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<DeviceWorkmanship>().eq("parent_id",deviceWorkmanshipVO.getParentId()));
                if (count==0){
                    DeviceWorkmanship deviceWorkmanshipParent=new DeviceWorkmanship();
                    deviceWorkmanshipParent.setId(deviceWorkmanshipVO.getParentId());
                    deviceWorkmanshipParent.setHasChild(false);
                    baseMapper.updateById(deviceWorkmanshipParent);
                }
            }

            return delCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int enable(Long id) {
        DeviceWorkmanshipVO deviceWorkmanshipVO=baseMapper.get4appById(id);
        if (deviceWorkmanshipVO!=null){
            DeviceWorkmanship deviceWorkmanship=new DeviceWorkmanship();
            deviceWorkmanship.setId(id);
            deviceWorkmanship.setIsDeleted(true);
            int updateCount=baseMapper.updateById(deviceWorkmanship);
            if (deviceWorkmanshipVO.getParentId()!=ROOT_ID){
                DeviceWorkmanship deviceCategoryParent=new DeviceWorkmanship();
                deviceCategoryParent.setId(deviceWorkmanshipVO.getParentId());
                deviceCategoryParent.setHasEnableChild(true);
                baseMapper.updateById(deviceCategoryParent);
            }

            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        DeviceWorkmanshipVO deviceWorkmanshipVO=baseMapper.get4appById(id);
        if (deviceWorkmanshipVO!=null){
            DeviceWorkmanship deviceWorkmanship=new DeviceWorkmanship();
            deviceWorkmanship.setId(id);
            deviceWorkmanship.setIsDeleted(false);
            int updateCount=baseMapper.updateById(deviceWorkmanship);
            if (deviceWorkmanshipVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<DeviceWorkmanship>().eq("parent_id",deviceWorkmanshipVO.getParentId()).eq("is_deleted",true));
                if (count==0){
                    DeviceWorkmanship deviceWorkshipParent=new DeviceWorkmanship();
                    deviceWorkshipParent.setId(deviceWorkmanshipVO.getParentId());
                    deviceWorkshipParent.setHasEnableChild(false);
                    baseMapper.updateById(deviceWorkshipParent);
                }
            }

            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int update(DeviceWorkmanshipForm deviceWorkmanshipForm) {
        DeviceWorkmanshipVO deviceWorkmanshipVO=baseMapper.get4appById(deviceWorkmanshipForm.getId());
        if (deviceWorkmanshipVO!=null){
            if (!deviceWorkmanshipVO.getParentId().equals(deviceWorkmanshipForm.getParentId())){
                boolean isUpdateEnable=deviceWorkmanshipVO.getIsDeleted().equals(deviceWorkmanshipForm.getIsDeleted());
                DeviceWorkmanship deviceWorkmanship=new DeviceWorkmanship();
                BeanUtils.copyProperties(deviceWorkmanshipForm,deviceWorkmanship);
                baseMapper.updateById(deviceWorkmanship);

                //update new parent category
                if (deviceWorkmanshipForm.getParentId()!=ROOT_ID){
                    DeviceWorkmanship parentDeviceWorkmanship=new DeviceWorkmanship();
                    parentDeviceWorkmanship.setId(deviceWorkmanshipForm.getParentId());
                    parentDeviceWorkmanship.setHasChild(true);
                    if (deviceWorkmanshipForm.getIsDeleted()){
                        parentDeviceWorkmanship.setHasEnableChild(true);
                    }
                    baseMapper.updateById(parentDeviceWorkmanship);
                }


                //update current db product category
                if (deviceWorkmanshipVO.getParentId()!=ROOT_ID){
                    int count=baseMapper.selectCount(new QueryWrapper<DeviceWorkmanship>().eq("parent_id",deviceWorkmanshipVO.getParentId()));
                    if (count==0){
                        DeviceWorkmanship deviceWorkmanshipParent=new DeviceWorkmanship();
                        deviceWorkmanshipParent.setId(deviceWorkmanshipVO.getParentId());
                        deviceWorkmanshipParent.setHasChild(false);
                        baseMapper.updateById(deviceWorkmanshipParent);
                    }
//                if (isUpdateEnable&&!deviceCategoryForm.getIsDeleted()){
                    count=baseMapper.selectCount(new QueryWrapper<DeviceWorkmanship>().eq("parent_id",deviceWorkmanshipVO.getParentId()).eq("is_deleted",true));
                    if (count==0){
                        DeviceWorkmanship deviceWorkmanshipParent=new DeviceWorkmanship();
                        deviceWorkmanshipParent.setId(deviceWorkmanshipVO.getParentId());
                        deviceWorkmanshipParent.setHasEnableChild(false);
                        baseMapper.updateById(deviceWorkmanshipParent);
                    }
                    //                }
                }


            }else{
                boolean isUpdateEnable=deviceWorkmanshipVO.getIsDeleted().equals(deviceWorkmanshipForm.getIsDeleted());
                DeviceWorkmanship deviceWorkmanship=new DeviceWorkmanship();
                BeanUtils.copyProperties(deviceWorkmanshipForm,deviceWorkmanship);
                baseMapper.updateById(deviceWorkmanship);
                if (isUpdateEnable&&!deviceWorkmanshipForm.getIsDeleted()&&deviceWorkmanshipForm.getParentId()!=ROOT_ID){
                    int count=baseMapper.selectCount(new QueryWrapper<DeviceWorkmanship>().eq("parent_id",deviceWorkmanshipVO.getParentId()).eq("is_deleted",true));
                    if (count==0){
                        DeviceWorkmanship deviceWorkmanshipParent=new DeviceWorkmanship();
                        deviceWorkmanshipParent.setId(deviceWorkmanshipVO.getParentId());
                        deviceWorkmanshipParent.setHasEnableChild(false);
                        baseMapper.updateById(deviceWorkmanshipParent);
                    }
                }

            }
        }
        return 0;
    }
}
